package controls
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.TextLineMetrics;
	
	import mx.controls.Button;
	import mx.controls.TextArea;
	import mx.core.UIComponent;
	
	[Event(name="change",type="flash.events.Event")]
	public class ModalText extends UIComponent
	{
		private var _itemsDirty:Boolean = false;
		private var txtArea:TextArea;
		private var modeBtn:Button;
		private var _text:String="Modal Text";
		private var _textPlacement:String = "left";
		public function ModalText()
		{
			super();
		}
		
		override protected function createChildren():void
		{
			super.createChildren();
			
			if(!txtArea)
			{
				txtArea = new TextArea;
				txtArea.explicitWidth=80;
				txtArea.editable = false;
				txtArea.text = _text;
				txtArea.addEventListener(Event.CHANGE,changeEventHandler);
				addChild(txtArea);
			}
			if(!modeBtn)
			{
				modeBtn = new Button;
				modeBtn.label = "Switch Edit";
				modeBtn.addEventListener(MouseEvent.CLICK,clickEventHandler);
				addChild(modeBtn);
			}
		}
		
		override protected function commitProperties():void
		{
			super.commitProperties();
			
			if(_itemsDirty)
			{
				txtArea.text = _text;
			}
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			var hGap:Number = 5;
			var vGap:Number = 0;
			var paddingTop:Number = 5;
			var paddingRight:Number = 5;
			var paddingBottom:Number = 5;
			var paddingLeft:Number = 5;
			
			var actWidth:Number = unscaledWidth-(paddingLeft+paddingRight);
			var actHeight:Number = unscaledHeight - (paddingTop+paddingBottom);
			var actArea:Rectangle = new Rectangle(paddingLeft,paddingTop,actWidth,actHeight);
			
			//Calculate the Button width and height based on its label value
			var labelDimension:TextLineMetrics = measureText(modeBtn.label);
			var btnWidth:Number = labelDimension.width+10;
			var btnHeight:Number = labelDimension.height+10;
			
			//Calculate TextArea width and height
			var taWidth:Number = actArea.width - btnWidth - hGap;
			var taHeight:Number = actArea.height;
			
			txtArea.width = taWidth;
			txtArea.height = taHeight;

			modeBtn.width = btnWidth;
			modeBtn.height = btnHeight;

			if(textPlacement == "right")
			{
				txtArea.x = actArea.x;
				txtArea.y = actArea.y;
				
				modeBtn.x = actArea.x + taWidth + hGap;
				modeBtn.y = actArea.y;
			
			}
			else
			{
				//left
				//or other values
				modeBtn.x = actArea.x;
				modeBtn.y = actArea.y;
			
				txtArea.x = actArea.x + btnWidth + hGap;
				txtArea.y = actArea.y;
			}
			graphics.lineStyle(1);
			graphics.drawRect(0,0,unscaledWidth,unscaledHeight);
		}
		public function set text(value:String):void
		{
			if(_text!=value)
			{
				_text = value;
				_itemsDirty = true;
				invalidateProperties();
				//Dispatch textChanged event
			}
		}
		public function get text():String
		{
			return txtArea.text;
		}
		
		
		public function set textPlacement(value:String):void
		{
			if(_textPlacement != value)
			{
				_textPlacement = value;
				invalidateDisplayList();
				//dispatch a textPlacement change event
			}
		}
		public function get textPlacement():String
		{
			return _textPlacement;
		}
		private function changeEventHandler(event:Event):void
		{
			dispatchEvent(new Event(Event.CHANGE));
		}
		private function clickEventHandler(event:MouseEvent):void
		{
			txtArea.editable = !txtArea.editable;
		}
	}
}